/**
 * Created by 121 on 2016/3/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts) {
    var id = getUrlPath(4);
    var index = function () {
        this.init();

    };
    index.prototype = {
        init: function () {
            this.event();
            this.fetch();
        },
        event: function () {
            var _this = this;
            //七天数据预览
            $.ajax({
                url: _global.url.api + 'site/data/yesterday',
                type: 'get',
                data: {id:id},
                success: function (res) {
                    var data = res.data;
                    $('#yesterday').html(data[1]['sums'] + '元');
                    $('#order').html(data[1]['orders'] + data[2]['orders'] + data[3]['orders'] + data[4]['orders'] + data[5]['orders'] + data[6]['orders'] + data[7]['orders'] + '个');
                    $('#amount').html((data[1]['sums'] + data[2]['sums'] + data[3]['sums'] + data[4]['sums'] + data[5]['sums'] + data[6]['sums'] + data[7]['sums']).toFixed(2) + '元');
                    a1 = (data[1]['sums'] - data[2]['sums']) / data[2]['sums'] * 100;
                    b1 = a1.toFixed(2) + "%";
                    a2 = (data[1]['sums'] - data[8]['sums'] ) / data[8]['sums'] * 100;
                    b2 = a2.toFixed(2) + "%";
                    if (a1 > 0 && isFinite(a1)) {
                        $('#qttb').html("<span class='glyphicon glyphicon-arrow-up' style='color: #ff0000'></span>" + b1);
                    } else if (a1 < 0 && isFinite(a1)) {
                        $('#qttb').html("<span class='glyphicon glyphicon-arrow-down' style='color:green'></span>" + b1);
                    } else {
                        $('#qttb').html("：暂无可比较数据");
                    }

                    if (a2 > 0 && isFinite(a2)) {
                        $('#sztb').html("<span class='glyphicon glyphicon-arrow-up' style='color: #ff0000'></span>" + b2);
                    } else if (a2 < 0 && isFinite(a2)) {
                        $('#sztb').html("<span class='glyphicon glyphicon-arrow-down' style='color:green'></span>" + b2);
                    } else {
                        $('#sztb').html("：暂无可比较数据");
                    }

                    $('#chart-one').highcharts({
                        chart: {
                            zoomType: 'xy'
                        },
                        title: {
                            text: '成交金额'
                        },
                        xAxis: [{
                            categories: [data[7]['date'], data[6]['date'], data[5]['date'], data[4]['date'], data[3]['date'], data[2]['date'], data[1]['date']]
                        }],
                        yAxis: [{ // Primary yAxis
                            labels: {
                                format: '{value}个',
                                style: {
                                    color: '#89A54E'
                                }
                            },
                            title: {
                                text: '成交笔数',
                                style: {
                                    color: '#89A54E'
                                }
                            }
                        }, { // Secondary yAxis
                            title: {
                                text: '销售额',
                                style: {
                                    color: '#4572A7'
                                }
                            },
                            labels: {
                                format: '{value}元',
                                style: {
                                    color: '#4572A7'
                                }
                            },
                            opposite: true
                        }],
                        tooltip: {
                            shared: true
                        },
                        legend: {
                            layout: 'vertical',
                            align: 'left',
                            x: 120,
                            verticalAlign: 'top',
                            y: 100,
                            floating: true,
                            backgroundColor: '#FFFFFF'
                        },
                        series: [{
                            name: '销售额',
                            color: '#4572A7',
                            type: 'column',
                            yAxis: 1,
                            data: [data[7]['sums'], data[6]['sums'], data[5]['sums'], data[4]['sums'], data[3]['sums'], data[2]['sums'], data[1]['sums']],
                            tooltip: {
                                valueSuffix: ' 元'
                            }

                        }, {
                            name: '成交笔数',
                            color: '#89A54E',
                            type: 'spline',
                            data: [data[7]['orders'], data[6]['orders'], data[5]['orders'], data[4]['orders'], data[3]['orders'], data[2]['orders'], data[1]['orders']],
                            tooltip: {
                                valueSuffix: '个'
                            }
                        }]
                    });
                }
            });
            //买家属性预览
            $.ajax({
                url: _global.url.api + 'site/data/property',
                type: 'get',
                data: {id:id},
                success: function (res) {
                    var data = res.data;

                    //买家性别饼图
                    var sex = [];
                    if (data['sex'].length > 0) {
                        for (var i = 0; i < data['sex'].length; i++) {
                            if (data['sex'][i]['sex'] == 0) {
                                sex[i] = ['未知', data['sex'][i]['sums']];
                                //alert(wx_pay);
                            } else if (data['sex'][i]['sex'] == 1) {
                                sex[i] = ['男', data['sex'][i]['sums']];
                            } else {
                                sex[i] = ['女', data['sex'][i]['sums']];
                            }

                        }
                    } else {
                        $('#chart-sex').remove();
                    }
                    $('#chart-sex').highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false
                        },
                        title: {
                            text: '买家性别分布'
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    color: '#000000',
                                    connectorColor: '#000000',
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                                }
                            }
                        },
                        series: [{
                            type: 'pie',
                            name: '买家性别分布',
                            data: sex
                        }]
                    });
                    //销售地区分布
                    var region = new Array();
                    var other = 0;
                    if (data['pay_region'].length < 10 && data['pay_region'].length > 0) {
                        for (var i = 0; i < data['pay_region'].length; i++) {
                            region[i] = [data['pay_region'][i]['name'], data['pay_region'][i]['sums']];
                        }
                    } else if (data['pay_region'].length > 10) {
                        region[0] = [data['pay_region'][0]['name'], data['pay_region'][0]['sums']];
                        region[1] = [data['pay_region'][1]['name'], data['pay_region'][1]['sums']];
                        region[2] = [data['pay_region'][2]['name'], data['pay_region'][2]['sums']];
                        region[3] = [data['pay_region'][3]['name'], data['pay_region'][3]['sums']];
                        region[4] = [data['pay_region'][4]['name'], data['pay_region'][4]['sums']];
                        region[5] = [data['pay_region'][5]['name'], data['pay_region'][5]['sums']];
                        region[6] = [data['pay_region'][6]['name'], data['pay_region'][6]['sums']];
                        region[7] = [data['pay_region'][7]['name'], data['pay_region'][7]['sums']];
                        region[8] = [data['pay_region'][8]['name'], data['pay_region'][8]['sums']];
                        for (var i = 9; i < data['pay-region'].length; i++) {
                            other += data['pay-region'][i]['sums'];
                        }
                        region[9] = ['other', other];
                    } else {
                        $('#chart-region').remove();
                        $('#property').html('对不起，昨日没有订单，所以没有更多买家属性数据，请稍后关注。').css('height', '100px');
                    }
                    $('#chart-region').highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false
                        },
                        title: {
                            text: '销售地区分布'
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    color: '#000000',
                                    connectorColor: '#000000',
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                                }
                            }
                        },
                        series: [{
                            type: 'pie',
                            name: '店铺销售区域分布情况',
                            data: region
                        }]
                    });

                    //支付方式饼图
                    var paid_type = new Array();
                    var wx_pay = 0;
                    var ali_pay = 0;
                    if (data['pay_type'].length > 0) {
                        for (var i = 0; i < data['pay_type'].length; i++) {
                            if ((data['pay_type'][i]['pay_type'] == 1) || (data['pay_type'][i]['pay_type'] == 2 )) {
                                wx_pay += data['pay_type'][i]['sums'];
                                //alert(wx_pay);
                            } else {
                                ali_pay += data['pay_type'][i]['sums'];
                            }

                        }
                    } else {
                        $('#chart-paid').remove();
                    }
                    $('#chart-paid').highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false
                        },
                        title: {
                            text: '支付方式'
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    color: '#000000',
                                    connectorColor: '#000000',
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                                }
                            }
                        },
                        series: [{
                            type: 'pie',
                            name: '支付方式分布',
                            data: [
                                ['微信支付', wx_pay],
                                ['支付宝支付', ali_pay]
                            ]
                        }]
                    });

                }
            });
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'site/data/hot_goods',
                condition: {id: id},
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + '',
                    event: {}
                },
                callback: function () {
                    loaded();
                    if (_this.collection.result.data.length < 1) {
                        //$('#hot-goods').hide();
                        $('#hot-goods').html('对不起，昨日没有订单，所以没有热卖商品展示数据，请稍后关注。').css('height', '100px');
                    }
                }
            });
            this.collection.fetch();
        }
    };
    new index();

});
